﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using CARMSBusiness.InterFace;
using Krystalware.SlickUpload;
using Krystalware.SlickUpload.Providers;
using NineRays.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS
{
    public class intFileNameGenerator : IFileNameGenerator
    {
        public string GenerateFileName(UploadedFile file)
        {
            return DateTime.Now.ToString("yyyyMMdd") + "/" + Guid.NewGuid() + new FileInfo(file.ClientName).Extension;
        }

        public static string GetDBFileName(string fileName)
        {
            return DateTime.Now.ToString("yyyyMMdd") + "/" + fileName;
        }
    }

    public static class EmptyType
    {
        public static string Empty = string.Empty;
        public static string PleaseSelect = "--Please Select--";
        public static string SelectAll = "--All--";
        public static string None = null;
    }
    public class ManualListControlItem
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

    public class ControlHelper
    {

        public static void BindReportCategoryList(ListControl objControl, String emptyType)
        {
            objControl.Items.Clear();
            var itemCollection = new ReportCategoryCollection();
            itemCollection.GetMulti(null, 0, new SortExpression { ReportCategoryFields.ReportCategoryName | SortOperator.Ascending });
            objControl.DataTextField = "ReportCategoryName";
            objControl.DataValueField = "ReportCategoryID";
            objControl.DataSource = itemCollection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }


        public static void BindPublicItemsByTypeID(ListControl objControl, int TypeID, String emptyType)
        {
            objControl.Items.Clear();
            var itemCollection = new DictionaryCollection();
            itemCollection.GetMulti(DictionaryFields.CategoryId == TypeID, 0, new SortExpression { DictionaryFields.SortIndex | SortOperator.Ascending });
            objControl.DataTextField = "Name";
            objControl.DataValueField = "OID";
            objControl.DataSource = itemCollection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }

        public static void BindParentPageList(ListControl objControl,  String emptyType)
        {
            objControl.Items.Clear();
            var itemCollection = new ActionCollection();
            itemCollection.GetMulti(ActionFields.ParentId == string.Empty, 0, new SortExpression { ActionFields.ActionName | SortOperator.Ascending });
            objControl.DataTextField = "ActionID";
            objControl.DataValueField = "ActionID";
            objControl.DataSource = itemCollection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }
        
 
        //public static void BindUserByRole(ListControl objControl,int roleID, string emptyType)
        //{
        //    objControl.Items.Clear();
        //    var condition = new PredicateExpression();
        //    condition.AddWithAnd(CurrencyFields.IsActive == true);

        //    var list =from item in SecurityBusiness.GetEmployeeByRoleID(roleID) select new ManualListControlItem
        //                                                                                   {
        //                                                                                        Name=item.FirstName+" "+item.LastName,
        //                                                                                        Value=item.EmployeeId.ToString()
        //                                                                                   };


        //    objControl.DataTextField = "Name";
        //    objControl.DataValueField = "Value";
        //    objControl.DataSource = list;
        //    objControl.DataBind();
        //    if (emptyType != null)
        //    {
        //        objControl.Items.Insert(0, new ListItem(emptyType, ""));
        //    }
        //}






        public static void BindTemplateList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var collection = new TemplateCollection();
            collection.GetMulti(null);
            objControl.DataTextField = "TemplateName";
            objControl.DataValueField = "TemplateID";
            objControl.DataSource = collection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }
        public static void BindUserList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var collection = new UserCollection();
            collection.GetMulti(UserFields.IsActived==true);
            objControl.DataTextField = "FullName";
            objControl.DataValueField = "UserID";
            objControl.DataSource = collection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }


  
        
 
     

        public static void BindCustomerList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var condition = new PredicateExpression();
            //condition.AddWithAnd(CustomerFields.IsActive == true);


            //var list = CustomerBusiness.GetCustomerList(condition, null, 0, 0);
            //var result = from entity in list orderby entity.FullName select entity;
            //objControl.DataTextField = "CustomerName";
            //objControl.DataValueField = "CustomerID";
            //objControl.DataSource = result;
            //objControl.DataBind();
            //if (emptyType != null)
            //{
            //    objControl.Items.Insert(0, new ListItem(emptyType, ""));
            //}
        }

         
        private static string GetCustomerName(int customerID)
        {
            return string.Empty;
        }

        private static string GetDataName(int dataID)
        {
            var customer = new DictionaryEntity(dataID);
            return customer.Name;
        }

        public static void BindCustomerListByType(ListControl objControl, int customerType, string emptyType)
        {
            //objControl.Items.Clear();
            //var condition = new PredicateExpression();
            //condition.AddWithAnd(CustomerFields.IsActive == true);
            //condition.AddWithAnd(CustomerFields.CustomerType == customerType);

            //var list = CustomerBusiness.GetCustomerList(condition, null, 0, 0);
            //var result = from entity in list orderby entity.CustomerName select entity;
            //objControl.DataTextField = "CustomerName";
            //objControl.DataValueField = "CustomerID";
            //objControl.DataSource = result;
            //objControl.DataBind();
            //if (emptyType != null)
            //{
            //    objControl.Items.Insert(0, new ListItem(emptyType, ""));
            //}
        }
 
        public static void BindDepartmentList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var condition = new PredicateExpression();
            //condition.AddWithAnd(DepartmentFields.IsActive == true);

            //var itemCollection = new DepartmentCollection();
            //itemCollection.GetMulti(condition, 0, new SortExpression { DepartmentFields.FullName | SortOperator.Ascending });
            //objControl.DataTextField = "DepartmentName";
            //objControl.DataValueField = "DepartmentId";
            //objControl.DataSource = itemCollection;
            //objControl.DataBind();
            //if (emptyType != null)
            //{
            //    objControl.Items.Insert(0, new ListItem(emptyType, ""));
            //}
        }
  
        public static void BindRoleList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();

            var itemCollection = new RoleCollection();
            itemCollection.GetMulti(null, 0, new SortExpression { RoleFields.RoleName | SortOperator.Ascending });
            objControl.DataTextField = "RoleName";
            objControl.DataValueField = "RoleId";
            objControl.DataSource = itemCollection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }
        public static void BindUserActive(ListControl objControl)
        {
            objControl.Items.Clear();
            var list = new List<ManualListControlItem>();
            list.Add(new ManualListControlItem { Name = "Active", Value = "1" });
            list.Add(new ManualListControlItem { Name = "Inactive", Value = "0" });
            objControl.DataTextField = "Name";
            objControl.DataValueField = "Value";
            objControl.DataSource = list;
            objControl.DataBind();
        }

          



        public static void SetRoleToUser(ListControl objControl, int userID)
        {

            var itemCollection = new UserRoleCollection();
            itemCollection.GetMulti(UserRoleFields.UserId == userID);
            foreach (var item in itemCollection)
            {
                foreach (ListItem listItem in objControl.Items)
                {
                    if (item.RoleId.ToString() == listItem.Value)
                    {
                        listItem.Selected = true;
                    }
                }
            }
        }

        public static void BindCountryList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var collection = new CountryCollection();
            collection.GetMulti(CountryFields.IsActive == true, 0,
                                new SortExpression { CountryFields.CountryName | SortOperator.Ascending });
            objControl.DataTextField = "CountryName";
            objControl.DataValueField = "CountryID";
            objControl.DataSource = collection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }
        public static void BindReportList(ListControl objControl, string emptyType)
        {
            objControl.Items.Clear();
            var collection = new ReportCategoryCollection();
            collection.GetMulti(null );
            objControl.DataTextField = "ReportCategoryName";
            objControl.DataValueField = "ReportCategoryID";
            objControl.DataSource = collection;
            objControl.DataBind();
            if (emptyType != null)
            {
                objControl.Items.Insert(0, new ListItem(emptyType, ""));
            }
        }



        //public static void BindProductList(ListControl objControl, string emptyType)
        //{
        //    objControl.Items.Clear();
        //    var collection = new ProductCollection();
        //    collection.GetMulti(ProductFields.IsActive == true, 0,
        //                        new SortExpression { ProductFields.FullName | SortOperator.Ascending });
        //    objControl.DataTextField = "ProductName";
        //    objControl.DataValueField = "ProductID";
        //    objControl.DataSource = collection;
        //    objControl.DataBind();
        //    if (emptyType != null)
        //    {
        //        objControl.Items.Insert(0, new ListItem(emptyType, ""));
        //    }
        //}



        public static void BindProductListByType(ListControl objControl, int productCategoryID, string emptyType)
        {
            objControl.Items.Clear();

            //var relation = new RelationCollection { ProductEntity.Relations.ProductCategoryToProductEntityUsingProductId };
            //var collection = new ProductCollection();

            //collection.GetMulti(ProductFields.IsActive == true & ProductCategoryToProductFields.ProductCategoryId == productCategoryID, 0,
            //                    new SortExpression { ProductFields.ProductName | SortOperator.Ascending }, relation);
            //objControl.DataTextField = "ProductName";
            //objControl.DataValueField = "ProductID";
            //objControl.DataSource = collection;
            //objControl.DataBind();
            //if (emptyType != null)
            //{
            //    objControl.Items.Insert(0, new ListItem(emptyType, ""));
            //}
        }


        #region  search condition and grid page setting

        public static void BindColumnTypeEntity(ListControl objControl)
        {
            objControl.Items.Clear();
            var list = new List<ColumnTypeEntity>();

            list.Add(ColumnType.ColumnInt);
            list.Add(ColumnType.ColumnString);
            list.Add(ColumnType.ColumnPublicItem);
            list.Add(ColumnType.ColumnRelativeTable);

            objControl.DataSource = list;
            objControl.DataTextField = "Name";
            objControl.DataValueField = "Value";
            objControl.DataBind();
        }

        public static void BindDictionaryCategory(ListControl objControl)
        {
            var collection = new DictionaryCategoryCollection();
            collection.GetMulti(null);

            objControl.DataSource = collection;
            objControl.DataTextField = "Name";
            objControl.DataValueField = "OID";
            objControl.DataBind();

            objControl.Items.Insert(0, new ListItem
            {
                Text = ColumnType.Empty.Name,
                Value = ColumnType.Empty.Value
            });
        }

        #endregion

    }

}
